package com.example.duomeiti;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
//
public class SdMp4Activity extends AppCompatActivity {
    MediaPlayer mediaPlayer;
    SurfaceView mSurfaceView;
    Button play,stop,pause;
    String path;
    SurfaceHolder sh;
    // Android 6.0后的版本，文件读写权限不但要在清单中加入，而且要在运行时动态申请。
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    // 在里面检查是否已经拥有读权限，没权限的话请用户同意授权，甚至把写权限一并打包进去。
    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }
    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sd_mp4);
        //授权验证
        verifyStoragePermissions(this);
        play=findViewById(R.id.play);
        pause=findViewById(R.id.pause);
        stop=findViewById(R.id.stop);
        mSurfaceView=findViewById(R.id.surfaceView);
        //Environment表示sd卡资源环境,Environment.DIRECTORY_DOWNLOADS  下载路径
//        path= Environment.getStorageDirectory(Environment.DIRECTORY_DOWNLOADS)+"/movie.mp4";
        mediaPlayer=new MediaPlayer();
        //装载视频文件
//        mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.movie);
//        mediaPlayer.setLooping(true);
        //播放按钮事件
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //播放按钮事件
                try{
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(path);    //设置数据源
                    mediaPlayer.setLooping(true);
                    sh=mSurfaceView.getHolder();//获取播放电影的容器
                    mediaPlayer.setDisplay(sh);//把电影显示到屏幕上
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    play.setEnabled(false); //把播放按钮设置为变灰
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        //暂停按钮事件
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if(!mediaPlayer.isPlaying()){   //没有播放
                        mediaPlayer.start();        //播放
                        pause.setText("暂停");
                    }else{
                        mediaPlayer.pause();        //暂停
                        pause.setText("播放");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        //停止按钮事件
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.stop();
                mediaPlayer.reset();
                //装载视频文件
                mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.movie);
                mediaPlayer.setLooping(true);
                play.setEnabled(true);//把播放按钮恢复
            }
        });
    }
}
